<?php

namespace app\index\service;

use app\common\model\AccountTokenGift;
use app\common\model\Member;
use app\common\model\Trade;
use app\index\dao\SourceCollectDao;
use app\index\dao\TradeDao;
use app\index\model\UM as UM;
use app\index\traits\SourceTrait;
use ary\Ary;

/**
 * Class SourceService
 * @package app\index\service
 */
class SourceService
{
    use SourceTrait;

    private Ary $ary;

    public function __construct(Ary $ary)
    {
        $this->ary = $ary;
    }

    /**
     * 案如云-案源广场列表
     * @param UM|null $user
     * @param $params
     * @return mixed
     */
    public function index(?UM $user, $params)
    {
        ary_page($params);

        if ($user) {
            $params['user_id'] = $user->id;
        }

        $response = $this->ary->getSourceList($params);

        $decodeContents = $response->getDecodeContents();

        $data = $decodeContents['data'];

        $this->wrapSourceList($user, $data);

        return $data;
    }

    private function wrapSourceList(?UM $user, array &$sources)
    {
        foreach ($sources as &$source) {
            $source['buy_status'] = 0;

            $source = $this->wrapSource($source);
        }

        if (!$user) {
            return;
        }

        $memberId = $user->id;
        $allSourceIds = ids($sources, 'source_id');

        if (empty($allSourceIds)) {
            return;
        }

        // 是不是我买过的
        $sourceIds = Trade::where('memberId', $memberId)
            ->whereIn('sourceId', $allSourceIds)
            ->where('tradeType', TRADE_SOURCE)
            ->where('payStatus', Trade::PAID)
            ->where('refundStatus', '<>', Trade::REFUNDED)
            ->field(['sourceId'])
            ->select()
            ->column('sourceId');
        if (!empty($sourceIds)) {
            foreach ($sources as &$source) {
                if (in_array($source['source_id'], $sourceIds)) {
                    $source['buy_status'] = 1;
                }
            }
        }


        // 剩下的线索
        $allSourceIds2 = array_values(array_diff($allSourceIds, $sourceIds));
        // 是不是我的同事买过的
        if (is_id($user->companyId) && !empty($allSourceIds2)) {

            // 查询我的同事
            $colleaguesIds = Member::where('companyId', $user->companyId)
                ->field(['id'])
                ->select()
                ->column('id');

            if (!empty($colleaguesIds)) {

                $sourceIds = Trade::whereIn('memberId', $colleaguesIds)
                    ->whereIn('sourceId', $allSourceIds2)
                    ->where('tradeType', TRADE_SOURCE)
                    ->where('payStatus', Trade::PAID)
                    ->where('refundStatus', '<>', Trade::REFUNDED)
                    ->field(['sourceId'])
                    ->select()
                    ->column('sourceId');

                if (!empty($sourceIds)) {
                    foreach ($sources as &$source) {
                        if (in_array($source['source_id'], $sourceIds)) {
                            $source['buy_status'] = 2;
                        }
                    }
                }
            }
        }

    }

    /**
     * 查询
     * @param UM $user
     * @param $params
     * @return AccountTokenGift
     */
    public function read(UM $user, $params)
    {
        $memberId = $user->id;
        $sourceId = $params['source_id'] ?? null;

        if ($user) {
            $params['user_id'] = $user->id;
        }

        $response = $this->ary->getSourceDetail($params);

        $decodeContents = $response->getDecodeContentsOrThrow('该案源已下架');

        $data = $decodeContents['data'];

        // 查询该线索是否被我收藏
        $data['collected'] = $this->getSourceCollectDao()->valueOfCollected($memberId, $sourceId);

        // 查询订单ID
        $data['trade_id'] = 0;

        $trade = $this->getTradeDao()->getTradeBySource($sourceId, $memberId);
        if ($trade) {
            $data['trade_id'] = $trade->id;
        }

        $data = $this->wrapSource($data);

        return $data;
    }

    private function getSourceCollectDao(): SourceCollectDao
    {
        return app(SourceCollectDao::class);
    }

    private function getTradeDao(): TradeDao
    {
        return app(TradeDao::class);
    }
}
